package at.kokoni.Koko_Ni.util;

import android.os.AsyncTask;
import android.util.Log;
import at.kokoni.Koko_Ni.entities.ApiResponse;
import at.kokoni.Koko_Ni.entities.Image;

public class ImageUtil extends AsyncTask<Object, Void, Image> {
	@Override
	protected Image doInBackground(Object... params) {
		if (params[0] instanceof String) {
			String func = (String) params[0];
			if (func.equalsIgnoreCase("image.id")) {
				if (params[1] instanceof Integer) {
					return getImageById((Integer) params[1]);
				}
			}
		}
		return null;
	}

	private static Image getImageById(int id) {
		UrlBuilder builder = new UrlBuilder("https://BASEURL/image/id/" + id);
		RestUtil util = new RestUtil(builder.getUrl());
		String json = util.get();
		if (json == null) {
			Log.w("Image.getImageById", "No Image found");
			return null;
		}
		ApiResponse response = ApiResponse.responseFromJson(json);
		if (response.getReturnValue() == 0) {
			Image tmp = Image.imageFromJson(response.getDataValue());
			tmp.setId(id);
			return tmp;
		} else {
			return null;
		}
	}
}
